<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jun 24, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );



RCoreImporter::cls("helper.RURLHelper");

$sql = "SELECT GROUP_CONCAT(`code` SEPARATOR '|') FROM site_language";
$langs = SysDatabase::loadResult($sql);

$mapString = "[lang:{$langs}]/app/act/task/option";
//$mapString = "[lang:vi|en]/app/act/task/option";
$returnURL = RURLHelper::getRequest($mapString);

//Map to Request Object
if (SYS_WORK_ZONE == 'front') {

	// checking the language
	if (SysRequest::getMethod() == 'GET') {
		if (!$returnURL['lang']) {
			$lang = $_REQUEST['lang'];
	
			if (!$lang) {
				// detect the IP
				function ip_address_to_number($IPaddress)
				{
					if ($IPaddress == "") {
						return 0;
					} else {
						$ips = split ("\.", "$IPaddress");
						return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
					}
				}			
	
				$lang = SystemSite::getLanguage();
				
				setcookie('lang', $lang, time() + 3600*24*7*365, '/');
			}
	
			RubikHelper::redirectToUrl(SYS_ROOT_URL.$lang."/".$_REQUEST['_rurq']);
		}
		else {
			setcookie('lang', $returnURL['lang'], time() + 3600*24*7*365, '/');
			$_REQUEST['lang'] = $returnURL['lang'];
		}
	}

	
	foreach ($returnURL as $mapKey => $mapReturn) {
		//echo "$mapKey => $mapReturn <br/>";
		$_GET[$mapKey] = $mapReturn;
		if (!key_exists($mapKey, $_REQUEST)) {
			$_REQUEST[$mapKey] = $mapReturn;
		}
	}
	// Clear $_rq
	unset($_GET['_rurq']);
	
//	RubikHelper::debugPrePrint($_REQUEST, true);
}
